package com.love.cloud.common.mybatis.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/***
 * 功能描述：分页参数获取
 * @author hongdongqu
 **/
@Component
public class PageUtils {
    /**
     * 当前页的字段名
     */
    private static final String PAGE_STR = "current";
    private static final Integer DEFAULT_PAGE = 1;
    // 页面数据数量
    /**
     * 页面数据数量
     */
    private static final String SIZE_STR = "size";
    private static final Integer DEFAULT_SIZE = 10;
    // 排序 是否正序排列，默认 true
    private static final String SORT_STR = "sort";
    private static final Boolean DEFAULT_SORT = true;
    private static final String SORT_NAME_STR = "sortName";
    //是否开启驼峰转化标识，默认是开启了
    private static final String SORT_UNDERSCORE_STR="underscore";
    @Autowired
    private HttpServletRequest request;
    /**
     /**
     * 获取分页数据
     */
    public Page page(){
        Page page = new Page();
        page.setCurrent(getPage());
        page.setSize(getSize());
        List<OrderItem> orderItems = getOrders();
        if(orderItems != null && orderItems.size() > 0){
            page.setOrders(orderItems);
        }

        return page;
    }

    /**
     * 自定义分页
     */
    public Map<String,Object> toPage(IPage page) {
        Map<String,Object> map = new LinkedHashMap<>(5);
        map.put("data",page.getRecords());
        map.put("total",page.getTotal());
        map.put("current",page.getCurrent());
        map.put("size",page.getSize());
        map.put("allPage",page.getPages());
        return map;
    }

    /**
     * 排序
     */
    private List<OrderItem> getOrders(){

        // 排序字段
        String sortName = getSortName();
        if(StringUtils.isBlank(sortName)){
            return null;
        }

        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(sortName);
        orderItem.setAsc(getSort());
        orderItems.add(orderItem);

        return orderItems;
    }

    /**
     * 处理分页数据
     */
    public void handlePageData(List<?> pageData, IPage page){
        //接收count数据
        Integer count = ((List<Integer>) pageData.get(1)).get(0);
        page.setRecords((List) pageData.get(0));
        page.setTotal(count);
    }
    /**
     * 获取page参数
     */
    private Integer getPage(){
        String page = request.getParameter(PAGE_STR);
        return StringUtils.isBlank(page) ? DEFAULT_PAGE : Integer.valueOf(page);
    }

    /**
     * 获取size参数
     */
    private Integer getSize(){
        String page = request.getParameter(SIZE_STR);
        return StringUtils.isBlank(page) ? DEFAULT_SIZE : Integer.valueOf(page);
    }

    /**
     * 获取sortName参数
     */
    private String getSortName(){
        String sortName=request.getParameter(SORT_NAME_STR);
        String underscore=request.getParameter(SORT_UNDERSCORE_STR);
        if("false".equals(underscore)){
            return sortName;
        }else{
            return  underscoreName(sortName);
        }
    }

    /**
     * 转换为下划线
     *
     * @param camelCaseName
     * @return
     */
    private static String underscoreName(String camelCaseName) {
        StringBuilder result = new StringBuilder();
        if (camelCaseName != null && camelCaseName.length() > 0) {
            result.append(camelCaseName.substring(0, 1).toLowerCase());
            for (int i = 1; i < camelCaseName.length(); i++) {
                char ch = camelCaseName.charAt(i);
                if (Character.isUpperCase(ch)) {
                    result.append("_");
                    result.append(Character.toLowerCase(ch));
                } else {
                    result.append(ch);
                }
            }
        }
        return result.toString();
    }

    /**
     * 获取sort参数 是否正序排列，默认 true
     */
    private boolean getSort(){

        String str = request.getParameter(SORT_STR);

        return StringUtils.isBlank(str) ? true : Boolean.valueOf(str);
    }
}
